package com.kintreda.ieco_server.module.place.web;

import com.kintreda.ieco_server.bean.account.Organization;
import com.kintreda.ieco_server.bean.attention.Attention;
import com.kintreda.ieco_server.bean.org.OrgSetting;
import com.kintreda.ieco_server.bean.place.Place;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.attention.service.IAttentionService;
import com.kintreda.ieco_server.module.organization.service.IOrgSettingService;
import com.kintreda.ieco_server.module.organization.service.IOrganizationService;
import com.kintreda.ieco_server.module.place.service.IPlaceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.Place.web
 * @Author ZhongYao_H
 * @Date 2019/5/8:9:32
 * @Year 2019 Month 05 Day 08
 * @Date_zh 2019 年 五月 星期三
 */

@Controller
@RequestMapping("/place")
public class PlaceController {


    @Autowired
    private IBaseRepository<Place,String> placeStringIBaseRepository;
    @Autowired
    private IPlaceService placeService;
    @Autowired
    private IOrgSettingService orgSettingService;
    @Autowired
    private IOrganizationService organizationService;
    @Autowired
    private IAttentionService attentionService;

    /**
     *
     *得到地图 获取坐标点
     * @return
     */
    @RequestMapping(value = "/map",method = RequestMethod.GET)
    public ModelAndView map(){
        return new ModelAndView("place/map");
    }

    @RequestMapping("/add")
    public ModelAndView addPlace(){
        return new ModelAndView("place/place-add");
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public Object add(Place place){
        try {

            if (placeService.getPlaceByName(place.getName())!=null){
                return R.error("错误!该名称已存在!");
            }

            Organization  organization = new Organization();
            organization.setType("12");
            organization.setNature("0");
            organization.setOrgStatus(2);
            organization.setName(place.getName());
            organization.setAddress(place.getAddress());
            organization.setLatitude(place.getLatitude());
            organization.setLongitude(place.getLongitude());
            organization.setPid("");
            organization.setIcon(place.getPhoto());
            organizationService.save(organization);
            OrgSetting orgSetting = new OrgSetting();
            orgSetting.setOrgId(organization.getId());
            orgSetting.setFrequency("单周一");
            place.setOrgId(organization.getId());
            orgSettingService.save(orgSetting);
            String save = placeService.save(place).toString();
            if (StringUtils.isNotBlank(save)){
                return R.ok("添加成功!");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error("错误! 稍后重试!");
        }

        return R.error("错误! 稍后重试!");
    }

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("place/place-list");
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(String id)  {

        try {

            organizationService.deleteByPK(id);
            List<Attention> attentionList = attentionService.getListByOrgId(id);
            attentionService.deleteAll(attentionList);
            placeStringIBaseRepository.excuteBySql("delete from place where orgId = ?" ,new Object[]{id});
            return R.ok("删除成功!");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("删除错误!稍后重试");
        }

    }

    /**
     *
     * 查询投放点
     * @param row
     * @param page
     * @return
     */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public  Object query(@RequestParam(name = "row",defaultValue = "10")Integer row, @RequestParam(name = "page",defaultValue = "1")Integer page
                        , String name){

        StringBuffer sql  = new StringBuffer("select id,name,longitude, latitude,address,createDate from place ");
        List<Object> params = new ArrayList<>();
        if (StringUtils.isNotBlank(name)){
            sql.append("where name like ? ");
            params.add("%"+name+"%");
        }
//        sql.append(" limit ? , ? ");
//        if(page==1){
//            params.add(0);
//        }else {
//            params.add(row*page);
//        }
//        params.add(row);
        List<Map<String, Object>> listBySql = placeStringIBaseRepository.findListBySql(sql.toString(), params.toArray());
        return listBySql;
    }





}
